Shader "UI/Outline"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _OutlineWidth ("Outline Width", Range(0, 1)) = 0.1
    }
 
    SubShader
    {
        Tags {"Queue"="Transparent" "RenderType"="Transparent"}
        Pass
        {
            Stencil
            {
                Ref 1
                Comp Always
                Pass Replace
            }
 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
 
            struct appdata
            {
                float4 vertex : POSITION;
            };
 
            struct v2f
            {
                float4 vertex : SV_POSITION;
            };
 
            fixed4 _Color;
            fixed4 _OutlineColor;
            float _OutlineWidth;
 
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
 
            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = _Color;
                fixed4 outlineCol = _OutlineColor;
 
                // 计算描边效果
                float2 pixelSize = _ScreenParams.xy / _ScreenParams.z;
                float2 offset = float2(_OutlineWidth, _OutlineWidth) * pixelSize;
 
                fixed4 outline = 0;
                outline.rgb = outlineCol.rgb;
                outline.a = 1;
 
                if (i.vertex.x < offset.x || i.vertex.x > 1 - offset.x ||
                    i.vertex.y < offset.y || i.vertex.y > 1 - offset.y)
                {
                    col = outlineCol;
                }
 col.a = _Color.a;
                return col;
            }
            ENDCG
        }
    }
}